package org.jimmy.tang.tools.mp3;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;

import org.jimmy.tang.util.StringUtils;

/**
 * @author jimmy
 * 
 */
public class MP3Manager {

	private static final String NEW_LOCATION = "C:/tjm/mp3/";

	// private static final String NEW_LOCATION = "/home/jimmy/mp3/";
	private static final String JOINT = " - ";
	private static int index = 0;
	// private static final String OLD_LOCATION = "/home/jimmy/mp3/";
	private static final String OLD_LOCATION = "F:/影音娱乐/";
	private static final String MP3_FILE = ".mp3";
	private static int limitation = 100;

	private static String generateMp3Name(Mp3 mp3)
			throws UnsupportedEncodingException {
		return NEW_LOCATION + (++index) + ". "
				+ StringUtils.toUTF8(mp3.getArtist().trim()) + JOINT
				+ StringUtils.toUTF8(mp3.getSongName() + MP3_FILE);
	}

	/**
	 * 
	 * @param mp3
	 * @throws IOException
	 */
	public static void writeMp3(Mp3 mp3) throws IOException {

		File file = new File(generateMp3Name(mp3));
		RandomAccessFile ran = mp3.getRan();
		byte[] byt = new byte[(int) ran.length()];
		// 必须返回到文件头，否则EOFException
		ran.seek(0);
		ran.readFully(byt);

		// Copy file
		RandomAccessFile ranCopy = new RandomAccessFile(file, "rw");
		ranCopy.write(byt);
		ranCopy.close();
	}

	/**
	 * 
	 * @param mp3
	 * @throws IOException
	 */
	public static void setMp3Info(Mp3 mp3) throws IOException {
		RandomAccessFile ran = mp3.getRan();
		byte[] buffer = new byte[128];
		ran.seek(ran.length() - 128);
		ran.read(buffer);

		String tag = new String(buffer, 0, 3);
		// 只有前三个字节是TAG才处理后面的字节
		if (tag.equalsIgnoreCase("TAG")) {
			mp3.setValid(true);

			mp3.setSongName(convertToString(buffer, 3, 30));
			mp3.setArtist(convertToString(buffer, 33, 30));
			mp3.setAlbum(convertToString(buffer, 63, 30));
			mp3.setYear(new String(buffer, 93, 4));
			mp3.setComment(convertToString(buffer, 97, 28));
			/*mp3.setSongName(StringUtils.getGBKString(buffer, 3, 30));
			mp3.setArtist(StringUtils.getGBKString(buffer, 33, 30));
			mp3.setAlbum(StringUtils.getGBKString(buffer, 63, 30));
			mp3.setYear(StringUtils.getGBKString(buffer, 93, 4));
			mp3.setComment(StringUtils.getGBKString(buffer, 97, 28));*/
			mp3.setR1(buffer[125]);
			mp3.setR2(buffer[126]);
			mp3.setR3(buffer[127]);

		} else {
			mp3.setValid(false);
		}
		System.out.println(mp3.getOldName()
				+ " is a valid mp3 file :"
				+ mp3.isValid()
				+ (mp3.isValid() ? "; and its Name:" + mp3.getSongName()
						+ "; artist:" + mp3.getArtist() + "; album:"
						+ mp3.getAlbum() : ""));
	}

	/**
	 * 
	 * @param byts
	 * @param start
	 * @param end
	 * @return
	 */
	private static String convertToString(byte[] byts, int offset, int length) {
		return StringUtils.getGBKString(byts, offset, length);

	}

	/**
	 * 
	 * @param namePath
	 * @throws IOException
	 */
	public static void writeMp3File(File file) throws IOException {
		Mp3 mp3 = new Mp3(file);
		setMp3Info(mp3);
		if (mp3.isValid()) {
			writeMp3(mp3);
		}
		mp3.close();
	}

	/**
	 * 
	 * @param folderPath
	 * @throws IOException
	 */
	public static void writeMp3Folder(String folderPath) throws IOException {

		File[] list = new File(folderPath).listFiles();
		if (index > limitation) {
			return;
		}
		for (File file : list) {
			if (file.isDirectory()) {
				System.out.println("----------" + file.getAbsolutePath());
				writeMp3Folder(file.getAbsolutePath());
			} else {
				writeMp3File(file);
			}
		}
	}

	/**
	 * 
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		// writeMp3File("/home/jimmy/xp/史逸欣 - 绿岛小夜曲.mp3");
		writeMp3Folder(OLD_LOCATION);
	}
}
